/**
 * 
 */
package com.onstar.cn.map.util;

import android.os.Handler;
import android.os.Message;

import com.amap.api.maps2d.AMap;
import com.amap.api.services.core.LatLonPoint;
import com.onstar.cn.map.common.Constants;
import com.onstar.cn.map.common.LocationType;
import com.onstar.cn.map.common.MapException;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.map.common.RouteInfo;
import com.onstar.cn.map.common.SearchResults;
import com.onstar.cn.map.listener.ORouteHandlerListener;
import com.onstar.cn.map.listener.OSearchHandlerListener;

/**
 * This class is to handle ansync message from searching and routing.
 * 
 * @author Qiying Gong
 * 
 * @Created 2013-4-27
 */
public class MessageHandler extends Handler {

	private AMap mapView;
	private ORouteHandlerListener routeListener;
	private OSearchHandlerListener searchListener;

	public MessageHandler(AMap mapView) {
		this.mapView = mapView;
	}

	public void registerRouteListener(ORouteHandlerListener listener) {
		this.routeListener = listener;
	}

	public void registerSearchListener(OSearchHandlerListener listener) {
		this.searchListener = listener;
	}

	public void removeRouteListener() {
		this.routeListener = null;
	}

	public void removeSearchListener() {
		this.searchListener = null;
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case Constants.GEO_CITY_SUCCESS:
			LatLonPoint point = (LatLonPoint) msg.obj;
			MapViewHelper.getInstance().animateTo(mapView, point);
			break;
		case Constants.CALCULATE_TEMP_ROUTE:
			RouteInfo info = (RouteInfo) msg.obj;
			if (routeListener != null) {
				routeListener.OnCalculateTempRoute(info.getId(),
						info.getStartItem(), info.getEndItem());
			}
			break;
		case Constants.ROUTE_COMPLETE:
			if (routeListener != null) {
				long id = (Long) msg.obj;
				routeListener.OnRouteComplete(id);
			}
			break;
		case Constants.ROUTE_START_POINT:
			if(routeListener != null) {
				LocationType type = (LocationType) msg.obj;
				routeListener.OnRouteStartPoint(type);
			}
			break;
		case Constants.ROUTE_END_POINT:
			if(routeListener != null) {
				LocationType type = (LocationType) msg.obj;
				routeListener.OnRouteEndPoint(type);
			}
			break;
		case Constants.ROUTE_SEARCH_ERROR:
			if (routeListener != null) {
				MapException exp = (MapException) msg.obj;
				routeListener.OnRouteErrors(exp.getId(),exp);
			}
			break;
		case Constants.SEARCH_RESULT:
			SearchResults result = (SearchResults) msg.obj;
			if (searchListener != null) {
				searchListener.OnSearchResults(result.getId(),
						result.getQueryType(), result.getResultList(),
						result.getPageIndex(), result.getTotalPages());
			}
			break;
		case Constants.REVERSE_GEOCODE_RESULT:
			PoiItemInfo poi = (PoiItemInfo) msg.obj;
			if (searchListener != null) {
				searchListener.OnReverseResults(poi.getThreadId(), poi);
			}
			break;
		case Constants.SEARCH_ERRORS:
			MapException exp = (MapException) msg.obj;
			if (searchListener != null) {
				searchListener.OnSearchErrors(exp.getId(),exp);
			}
		}
	}
}
